package com.wp.common;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author 盼
 *用于用户密码的加密与保存
 */
public class MD5Util {
	
	//对密码加密，返回32位长度的密码字符串保存进数据库
	public static String makeMode(String pwd){
		if(pwd==null)return "";
		StringBuffer sb = new StringBuffer();
		String res = "";
		char[] arrs = {'2','0','1','8','-','1','2','-','0','7','-','0','0','-','1','0','-','5','8'};
		try {
			MessageDigest digest = MessageDigest.getInstance("MD5");
			byte[] bytes = digest.digest(pwd.getBytes());
			for(byte b:bytes){
				sb.append(b);
			}
			res = sb.toString();
			if(res.length()>=32){
				res = res.substring(0, 32);
			}else{
				int i = 0;
				while(res.length()<32){
					res = res + arrs[i];
					i++;
					if(i>arrs.length){
						i=0;
					}
				}
			}
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return res; 
	}
}
